// 资产库可用资产信息
var purchaseProductDatas = new Array();
// 采购记录
var proc1Columns = [{
    id: 'commodityCateName',
    title: '类目',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'commodityClassName',
    title: '类别',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'name',
    title: '名称',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'descAndSpec',
    title: '品名及规格',
    width: 200,
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'quantity',
    title: '数量',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'singlePrice',
    title: '单价',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
    }
}, {
    id: 'estimatedAmount',
    title: '预计金额',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
    }
}, {
    id: 'remark',
    title: '备注',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    width: 130,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-info" onclick="editPurchase(\'' + record.id + '\');"><i class="fa fa-edit"></i>  编辑</button>';
        content += '  ';
        content += '<button class="btn btn-xs btn-danger" onclick="deletePurchase(\'' + record.id + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var priceOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '采购比价单列表信息',
    datas: purchaseProductDatas,
    columns: proc1Columns,
    gridContainer: 'purchaselogContainer',
    tools: '',
    pageSize: 10,
    pageSizeLimit: [10, 15, 20]
};
var purchaseId = '';
var purchaseGrid = $.fn.dlshouwen.grid.init(priceOption);
var asset_id = '';
var productSelectDatagrid;
var classSet = {};
var lidColumns = [{
    id: 'asset_id',
    title: '资产编号',
    width: 170,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'city_location',
    title: '放置地',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'comments',
    title: '资产描述',
    type: 'string',
    columnClass: 'text-left',
    // hideType:'lg|md|sm|xs',
}, {
    id: 'operation',
    title: '操作',
    width: 60,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        for (var i in library1Grid.exhibitDatas) {
            if (library1Grid.exhibitDatas[i].asset_id == record.asset_id) {
                content += '<button class="btn btn-xs btn-info" onclick="applyProduct(\'' + record.asset_id + '\',this);" disabled>已申请</button>';
                return content;
            }
        }
        content += '<button class="btn btn-xs btn-info" onclick="applyProduct(\'' + record.asset_id + '\',this);"><i class="icon-ok"></i>  申请</button>';
        return content;
    }
}];
var library1Columns = [
    {
        id: 'asset_style1',
        title: '资产类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'asset_style2',
        title: '资产类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'asset_id',
        title: '资产编号',
        width: 170,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'comments',
        title: '资产描述',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'asset_status',
        title: '状态',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return "空闲";
        }
    }, {
        id: 'create_user_id',
        title: '归属人',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'pro_name',
        title: '归属项目',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'city_location',
        title: '放置地',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'operation',
        title: '操作',
        width: 50,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-danger" onclick="deleteProduct(\'' + record.asset_id + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
            return content;
        }
    }
];
var productDatas = new Array();
var $$scope;
var $CertusFormAjax;
var $CertusJsonAjax;
var $fileListService;
var $backlogPersonList;
var library1Grid = productListInit(productDatas);

// 请购单列表
var listColumns = [{
    id: 'brCode',
    title: '请购单编号',
    width: 250,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'projName',
    title: '项目名称',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'applicantName',
    title: '申请人',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'createTime',
    title: '申请时间',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var date = new Date(record.createTime);
        var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy/MM/dd hh:mm");
        return content;
    }
}, {
    id: 'affiliationCode',
    title: '归属分公司',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var affiliationCode = record.affiliationCode;
        if (affiliationCode == "STSNJ") {
            return "南京";
        } else if (affiliationCode == "STSBJ") {
            return "北京";
        } else if (affiliationCode == "STSGD") {
            return "广州";
        } else if (affiliationCode == "STSSH") {
            return "上海";
        }
    }
}, {
    id: 'applicationCode',
    title: '申请用途',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var affiliationCode = record.applicationCode;
        ;
        if (affiliationCode == "SY") {
            return "商用";
        } else if (affiliationCode == "ZY") {
            return "自用";
        } else if (affiliationCode == "QT") {
            return "其他";
        }
    }
}, {
    id: 'applyInstruction',
    title: '申请说明',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'purchaseAccomplish',
    title: '审批状态',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        if (record.purchaseAccomplish == "yes") {
            return "通过";
        }

        return "审批中";
    }
}, {
    id: 'operation',
    title: '操作',
    width: 60,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<a class="btn btn-xs btn-primary" href="#/selPurchase/' + record.brCode + '"><i class="icon-eye-open"></i>  查看</a>';
        return content;
    }
}];

function isSaveProjName(element) {
    return element.projName.indexOf(nowProjName) > -1;
}

function disPurchase(a, b) {
    $$scope.modalContent = {};

    $$scope.modalContent = a;

    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}
// 请购信息展示隐藏效果
function shadbox() {
    var status = $(".shadbox").css('display');
    $$scope.applyItem = {};
    $$scope.$apply();
    if (status == "none") {
        $(".shadbox").show();
    } else {
        $(".shadbox").hide();
    }
}

// 上传文件展示隐藏效果
function shuplod() {
    var status = $(".shuplod").css('display');
    if (status == "none") {
        $(".shuplod").show();
    } else {
        $(".shuplod").hide();
    }
}

// 点击删除按钮弹窗提示效果
function del() {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        layer.msg('删除成功');
    });
}

// 请购信息-选择大类显示隐藏效果
$(function () {
    $(".typesel").change(function () {
        if ($(this).val() == 1) {
            $(".typesel-option").show();
        } else if ($(this).val() == 2) {
            $(".typesel-option").hide();
        } else if ($(this).val() == 3) {
            $(".typesel-option").hide();
        }
    })
});

function deleteProduct(productNumber) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        asset_id = productNumber;
        for (var i in productDatas) {

            if (productDatas[i].asset_id == productNumber) {
                productDatas = productDatas.filter(isSamea)
                library1Grid.exhibitDatas = productDatas;
                library1Grid.reload(false);
            }
        }
        layer.msg('删除成功');
    });
}

// 显示隐藏采购表单
function shproc() {
    $$scope.applyItem = {};
    $$scope.$apply();
    $('.hdproc').show();
}

//隐藏采购表单
function hdproc() {
    $('.hdproc').hide();
}

function applyProduct(recordString, me) {
    var exhibitDatas = productSelectDatagrid.exhibitDatas;
    for (var i in exhibitDatas) {
        if (exhibitDatas[i].asset_id == recordString) {
            var flag = true;
            for (var j in productDatas) {
                if (productDatas[j].asset_id == recordString) {
                    flag = false;
                }
            }
            if (flag) {
                productDatas.push(exhibitDatas[i]);
            }
            library1Grid.exhibitDatas = productDatas;
            $(me).html("").html('已申请');
            library1Grid.reload(false);
        }
    }

}

function isSamea(element) {
    return element.asset_id != asset_id;
}

function editPurchase(id) {
    for (var i in purchaseProductDatas) {
        if (purchaseProductDatas[i].id == id) {
            $$scope.applyItem = purchaseProductDatas[i];
            $$scope.$apply();
            $('.shadbox').show();
            return
        }
    }
}

function isSamePurchase(element) {
    return element.id != purchaseId;
}

function deletePurchase(id) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        purchaseId = id;
        purchaseProductDatas = purchaseProductDatas.filter(isSamePurchase);
        purchaseGrid.exhibitDatas = purchaseProductDatas;
        purchaseGrid.refresh(false);
        layer.msg('删除成功');
    });
   
}

function productListInit(productDatas) {
    var library1Option = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '资产库借货信息',
        datas: productDatas,
        columns: library1Columns,
        gridContainer: 'assetslogContainer',
        tools: '',
        pageSize: 10,
        pageSizeLimit: [10, 15, 20]
    };

    var library1Grid = $.fn.dlshouwen.grid.init(library1Option);
    library1Grid.load();
    return library1Grid;
}

function listUploadFilePurchase() {
    $$scope.modalContent = {};

    $$scope.modalContent = $$scope.applyInfo.attachmentFiles;

    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}

//文件列表下载
function downLoad(id) {
    var fileID = $(id).parent().parent().find(".fileDownloadPath").val();
    $CertusFormAjax.download("downloadFile", "fileId=" + fileID);
}